package com.example.snakefinal;

import static android.Manifest.permission.RECORD_AUDIO;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.widget.Button;

import androidx.core.app.ActivityCompat;

public class SnakeActivity extends Activity {

    // Declare an instance of SnakeGame, but don't initialize it here
    SnakeGame mSnakeGame;


    // Set up the activity
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);

        if (ActivityCompat.checkSelfPermission(getApplicationContext(), RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            System.out.println("权限不足。");
            ActivityCompat.requestPermissions(this, new String[]{RECORD_AUDIO}, RECORD_AUDIO.hashCode());
            return;
        }
        // Create the "Choose Skin" button
        Button chooseSkinButton = findViewById(R.id.btnChooseSkin);
        Button startGameButton = findViewById(R.id.btnStartGame);
        Button startGameButton2 = findViewById(R.id.btnStartGame2);
        Button chooseWayMethod = findViewById(R.id.btnChooseWayMethod);

        chooseSkinButton.setOnClickListener(v -> {
            // Navigate to the SnakeSkinChoice activity
            Intent intent = new Intent(SnakeActivity.this, SnakeSkinChoice.class);
            startActivity(intent);
        });

        chooseWayMethod.setOnClickListener(view -> {
            // Navigate to the SnakeSkinChoice activity
            Intent intent = new Intent(SnakeActivity.this, SnakeWayMethodChoice.class);
            startActivity(intent);
        });

        // Start Game button click listener
        startGameButton2.setOnClickListener(v -> {
            Intent intent = new Intent(SnakeActivity.this, SnakeMultiplayer.class);
            startActivity(intent);
        });

        // Start Game button click listener
        startGameButton.setOnClickListener(v -> {
            // Get the pixel dimensions of the screen
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            // Create a new instance of the SnakeGame class
            mSnakeGame = new SnakeGame(this, size,0);
            // Set SnakeGame as the content view of the activity
            setContentView(mSnakeGame);
            // Resume the game when the user starts the game
            mSnakeGame.resume();
            SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("SnakeGamePrefs", Context.MODE_PRIVATE);
            int skinResource = sharedPref.getInt("snakeWayMethod", 1);
            mSnakeGame.initWayMethod(skinResource);
            // 注册陀螺仪。
            if (skinResource == 2) {
                SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
                Sensor gyroscopeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
                sensorManager.registerListener(new ShakeSensor(mSnakeGame), gyroscopeSensor, SensorManager.SENSOR_DELAY_NORMAL);
            }
        });

    }

    // Only resume the game if it has been initialized
    @Override
    protected void onResume() {
        super.onResume();
        if (mSnakeGame != null) {
            mSnakeGame.resume();
        }
    }

    // Pause the game if it has been initialized
    @Override
    protected void onPause() {
        super.onPause();
        if (mSnakeGame != null) {
            mSnakeGame.pause();
        }
    }
}
